package com.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.reggie.common.R;
import com.reggie.dto.OrdersDto;
import com.reggie.pojo.AddressBook;
import com.reggie.pojo.OrderDetail;
import com.reggie.pojo.Orders;
import com.reggie.pojo.ShoppingCart;
import com.reggie.service.impl.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Transactional
public class OrderController {

    @Autowired
    private OrdersServiceImpl ordersService;

    @Autowired
    private ShoppingCartServiceImpl shoppingCartService;

    @Autowired
    private OrderDetailServiceImpl orderDetailService;
    @Autowired
    private AddressBookServiceImpl addressBookService;

    @Autowired
    private UserServiceImpl userService;
    @PostMapping("/submit")
    @Transactional
    public R<String> addOrder(@RequestBody Orders orders, HttpServletRequest request){
        System.out.println(orders);
        Long user_id =(Long) request.getSession().getAttribute("user");
        orders.setUserId(user_id);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        AddressBook addressBook = addressBookService.selectByUserId(
                addressBookService.selectById(orders.getAddressBookId()).getUserId());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(userService.selectById(user_id).getName());
        orders.setUserName(addressBook.getConsignee());
        orders.setConsignee(addressBook.getConsignee());
        //使用这个用户的id查一下他的购物车里有什么
        List<ShoppingCart> shoppingCarts = shoppingCartService.selectByUserId(user_id);
        List<OrderDetail> list = new ArrayList<>();
        long accountAll = 0L;
        for (ShoppingCart shoppingCart : shoppingCarts) {
            OrderDetail od = new OrderDetail();
            //看看这个是套餐还是菜品
            od.setImage(shoppingCart.getImage());
            od.setOrderId(orders.getId());
            od.setDishId(shoppingCart.getDishId());
            od.setSetmealId(shoppingCart.getSetmealId());
            od.setDishFlavor(shoppingCart.getDishFlavor());
            od.setAmount(shoppingCart.getAmount());
            od.setName(shoppingCart.getName());
            od.setAmount(shoppingCart.getAmount());
            BigDecimal amount = shoppingCart.getAmount();
            long account = amount.longValue();
            accountAll += account;
            list.add(od);
        }
        orders.setAmount(BigDecimal.valueOf(accountAll));
        ordersService.addOrders(orders);
        System.out.println(accountAll);
        for (OrderDetail detail : list) {
            detail.setOrderId(orders.getId());
            orderDetailService.addOrderDetail(detail);
        }
        //都添加完成后将这个用户的购物车清空
        shoppingCartService.deleteByUserId(user_id);
        return R.success("添加成功");
    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> selectByPageFront(Integer page, Integer pageSize,HttpServletRequest request){
        //只查看当前登录在session中的这个对象的购物车信息
        Page<Orders> ordersPage = ordersService.selectByPage(page, pageSize,request);
        System.out.println(ordersPage.getPages());
        //这个用户的所有订单
        List<Orders> orders = ordersPage.getRecords();
        List<OrdersDto> list = orders.stream().map(item -> {
            OrdersDto dto = new OrdersDto();
            BeanUtils.copyProperties(item, dto);
            //其中的一个订单,这个订单的所有的细节
            List<OrderDetail> orderDetails = orderDetailService.selectByOrderId(item.getId());
            dto.setOrderDetails(orderDetails);
            return dto;
        }).collect(Collectors.toList());
        Page<OrdersDto> result = new Page<>();
        result.setRecords(list);
        return R.success(result);
    }

    @GetMapping("/page")
    public R<Page<Orders>> selectByPageBackend(Integer page,Integer pageSize,Long number,String beginTime,String endTime){
        System.out.println(beginTime+":"+endTime);
        Page<Orders> ordersPage = ordersService.selectByPageAll(page, pageSize,number,beginTime,endTime);
        return R.success(ordersPage);
    }
}
